<?

$check_me_authed = "NO_OK";

if(($jid = $_POST["radioview"]) || ($jid = $_POST["job_id_posted"]) ){

	$Db = new oracleDb;
  $Db->init($_SESSION["ORCL"],
             $_SESSION["OPASS"],
             $_SESSION["SID"],
             _NO_COMMENT);

	if ($Db->connect()) {

  	$srcDbHdr        = "C_B_HDR";
    $srcDbDtl        = "C_B_DTL";
    $srcDbSubDtl     = "C_B_SUB_DTL";
    $srcDbHdrLog     = "LOG_C_B_HDR";
    $srcDbSequence   = "MST_SEQUENCE";

		$_SESSION["jid"]  = $_POST["radioview"];
		$jobId            = &$_SESSION["jid"];

		/**
		* Fetch global header
		* And check the status if it is valid
		* where STATUS = _NOT_ACTIVE
		*
		*/
		$CLFetchers->fetchLstDataHeader($Db,
                                    $jobId,
                                    $SourceDb,
                                    $nHeader,
                                    $lstHeader);

		if($lstHeader["STATUS"][0] == _NOT_ACTIVE ||
		   $lstHeader["STATUS"][0] == _CANCELED){

  		/**
  		* Init the header vars
  		*
  		*/
  		$_POST["trDateEnt"]       = date("d-m-Y",
  		                                 strtotime($lstHeader["TR_DATE"][0]));
  	  $_POST["cbCode"]         = $lstHeader["C_B_CODE"][0];
  	  $_POST["cbName"]         = $lstHeader["C_B_NAME"][0];
  	  $_POST["typeEnt"]        = $lstHeader["IN_OUT"][0];
  	  $_POST["currencyEnt"]    = $lstHeader["CURRENCY"][0];
  	  $_POST["rateEnt"]        = $lstHeader["RATE"][0];
  	  $_POST["suppCstCodeEnt"] = $lstHeader["SUPPLIER_CUSTOMER"][0];
  	  $_POST["suppCstNameEnt"] = $lstHeader["SUPPLIER_CUSTOMER_NAME"][0];
  	  $_POST["txEnt"]          = $lstHeader["CASH_BANK_TX_NAME"][0];
  	  $_POST["memoEnt"]        = $lstHeader["MEMO"][0];

  		/**
  		* Init the detail
  		*
  		*/
  		$CLFetchers->fetchLstDataDetail($Db,
                                      $jobId,
                                      $srcDbDtl,
                                      $nDetail,
                                      $lstDetail);

      $_POST["totAmount"] = $lstDetail["TOT_AMOUNT"][0];
      $_POST["totNet"]    = $lstDetail["TOT_NET"][0];

      $CLFetchers->fetchLstDataSubDetail($Db,
                                         $jobId,
                                         $SourceDb,
                                         $nSubDetail,
                                         $lstSubDetail);

  		for($iRow = 0; $iRow < $nSubDetail; $iRow++){

        $_POST["glAccEnt"][$iRow]         = $lstSubDetail["GL_ACC_NAME"][$iRow];
        $_POST["glAccCodeEnt"][$iRow]     = $lstSubDetail["GL_ACC"][$iRow];
        $_POST["glSubAccEnt"][$iRow]      = $lstSubDetail["GL_SUB_ACC_NAME"][$iRow];
        $_POST["glSubAccCodeEnt"][$iRow]  = $lstSubDetail["GL_SUB_ACC"][$iRow];
        $_POST["descpEnt"][$iRow]         = $lstSubDetail["DESCRIPTION"][$iRow];
        $_POST["amountEnt"][$iRow]        = $lstSubDetail["AMOUNT"][$iRow];
        $_POST["netEnt"][$iRow]           = $lstSubDetail["NET"][$iRow];
  		}

  		/**
  		* Check if this tx isn't canceled
  		* YES: it is modify
  		* NO : Add new entry with this copy
  		*
  		*/
  		if($lstHeader["STATUS"][0] != _CANCELED){

  		  $_SESSION["modify_mode"]= "Y";
  		}else{

  		  $_SESSION["modify_mode"]= "N";
  		}

  		/**
  		 * process transfer
  		 * Mutation --> Tmp
  		 *
  		 * if
  		 * invoice
  		 * cancel dp
  		 */
  		$_SESSION["tmpJid"] = $GProcessors->getTmpJid();

		  /**
       * transfer process
       * real --> tmp
       */
      switch($lstHeader["IN_OUT"][0]){

        case _CASH_BANK_IN:

          switch($lstHeader["CASH_BANK_TX_NAME"][0]){

            case _CASH_BANK_INVOICE_NAME:


              break;

            case _CASH_BANK_CCL_DP_NAME:


              break;

            default:
              $Misc->usrDieJs("Error: Unknown Tx");
          }
          break;

        case _CASH_BANK_OUT:

          switch($lstHeader["CASH_BANK_TX_NAME"][0]){

            case _CASH_BANK_INVOICE_NAME:

              $CLHandlers->transIvcRealToTmpSupplier($Db,
                                                     $jobId,
                                                     $_SESSION["tmpJid"],
                                                     $SourceDb);
              break;

            case _CASH_BANK_CCL_DP_NAME:

              $CLHandlers->transCclDpRealToTmpSupplier($Db,
                                                       $jobId,
                                                       $_SESSION["tmpJid"],
                                                       $SourceDb);
              break;

            default:
              $Misc->usrDieJs("Error: Unknown Tx");
          }
          break;

        default:
          $Misc->usrDieJs("Error: Unknown Type");
      }

  		/**
  		* This is only to by pass
  		* the entry_detail precheck
  		*
  		*/
  		//$_POST["entry"]         = "byPass";
  		$_SESSION["init_num"]   = $nSubDetail;

		  $Db->commit();

      if($Db->getStatus()){

        /* Do nothing */
      }else{

        $Misc->usrDieJs("Transfers to Temp Failed");
      }

  		$Db->dc();

  		require("entry_restore_me.php");

  	}else{

  	  $Misc->usrDieJs("Tx can't be modified, it has been approved");
  	}
	}else{

	  $Misc->usrDieJs(_DB_CONN_ERROR);
	}
}else{

	$Misc->usrDieJs("Error:Cant retrieved job_id");
}

?>